package com.liuzy.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具类
 * @author liuzy
 * @since 2015年10月17日
 */
public class Util {
	/** 打印日志 */
	public static void log(String head, String msg) {
		System.out.println(String.format("[%5s] %s", head, msg));
	}
	/** 从请求中取出所有参数 */
	public static Map<String, String> getAllParams(HttpServletRequest request) {
		Map<String, String> map = new HashMap<>();
		Map<String, String[]> allMap = request.getParameterMap();
		for (String key : request.getParameterMap().keySet())
			map.put(key, allMap.get(key)[0]);
		return map;
	}
	/** 从请求中取出所有参数 */
	public static String getAllParams2kv(HttpServletRequest req) {
		return map2kv(getAllParams(req));
	}
	/** 从请求中过滤出参数 */
	public static Map<String, String> getParams(String without, HttpServletRequest request) {
		Map<String, String> map = new HashMap<>();
		List<String> withoutList = Arrays.asList(without.split("\\|"));
		if (without == null || without.isEmpty())
			return map;
		Map<String, String> allMap = getAllParams(request);
		for (String key : allMap.keySet()) {
			if (!withoutList.contains(key))
				map.put(key, allMap.get(key));
		}
		return map;
	}
	/** 把&键值字符串转为Map */
	public static Map<String, String> kv2map(String query) {
		Map<String, String> map = new HashMap<>();
		if (query == null || !query.contains("\\&")) {
			String[] k_v = query.split("&");
			for (String str : k_v) {
				if (str.contains("=")) {
					String[] kv = str.split("=");
					map.put(kv[0], kv[1]);
				} else {
					map.put(str, "");
				}
			}
		}
		return map;
	}
	/** 把Map转为&键值字符串 */
	public static String map2kv(Map<String, String> map) {
		if (map == null || map.isEmpty())
			return "";
		StringBuilder sb = new StringBuilder("?");
		for (String key : map.keySet())
			sb.append(key).append("=").append(map.get(key)).append("&");
		return sb.substring(0, sb.length() - 1);
	}
	/** 把字符串中的\\uxxxx转为中文 */
	public static String Unicode2Chinese(String str) {
		while (true) {
			int index = -1;
			if ((index = str.indexOf("\\u")) == -1)
				break;
			String un = str.substring(index, index + 6);
			String ch = convert(un);
			str = str.replaceAll("\\" + un, ch);
		}
		return str;
	}
	/** 把单个\\uxxxx转为中文 */
	private static String convert(String un) {
		char ch;
		int len = un.length();
		StringBuffer sb = new StringBuffer(len);
		for (int x = 0; x < len;) {
			ch = un.charAt(x++);
			if (ch == '\\') {
				ch = un.charAt(x++);
				if (ch == 'u') {
					int value = 0;
					for (int i = 0; i < 4; i++) {
						ch = un.charAt(x++);
						switch (ch) {
							case '0' :
							case '1' :
							case '2' :
							case '3' :
							case '4' :
							case '5' :
							case '6' :
							case '7' :
							case '8' :
							case '9' :
								value = (value << 4) + ch - '0';
								break;
							case 'a' :
							case 'b' :
							case 'c' :
							case 'd' :
							case 'e' :
							case 'f' :
								value = (value << 4) + 10 + ch - 'a';
								break;
							case 'A' :
							case 'B' :
							case 'C' :
							case 'D' :
							case 'E' :
							case 'F' :
								value = (value << 4) + 10 + ch - 'A';
								break;
							default :
								throw new IllegalArgumentException("Malformed \\uxxxx encoding.");
						}
					}
					sb.append((char) value);
				} else {
					sb.append("\\" + ch);
				}
			} else
				sb.append(ch);
		}
		return sb.toString();
	}
}
